﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;

namespace DeepTalkDBSerice
{
    [ServiceContract]
    public interface IDTDB
    {
        /// <summary>
        /// Returns up to maxtalks specs for unrendered talks. Usually enough info for a quick display, but no more than that.
        /// The return list is ordered by date of submission to the database.
        /// </summary>
        /// <param name="maxtalks">Up to how many talks should be returned?</param>
        /// <returns></returns>
        [OperationContract]
        BasicTalkInfo[] GetUnrenderedTalks(int maxtalks);

        /// <summary>
        /// Returns up to max talks specs for rendered talks. The order is by date of
        /// the meeting, most recent first.
        /// </summary>
        /// <param name="maxtalks"></param>
        /// <returns></returns>
        [OperationContract]
        BasicTalkInfo[] GetRenderedTalks(int maxtalks);

        /// <summary>
        /// Returns some number of recently rendered talks, by rendering order,
        /// not by conference date.
        /// </summary>
        /// <param name="maxtalks"></param>
        /// <returns></returns>
        [OperationContract]
        BasicTalkInfo[] GetRecentlyRenderedTalks(int maxtalks);

        /// <summary>
        /// Submit a new talk to be rendered. Give the xml for AgendaInfo along with a title and a start date to
        /// populate the DB with. If the talk is already in the DB then this will have no effect. Otherwise it is
        /// inserted and anyone listending for unrendered talks is notified.
        /// </summary>
        /// <param name="agendaInfo"></param>
        /// <param name="title"></param>
        /// <param name="startTime"></param>
        [OperationContract]
        void SubmitNewTalk(string agendaInfo, string title, DateTime startTime);

        /// <summary>
        /// Given the unique agenda info, find the talk in the db. Return -1 if it can't.
        /// </summary>
        /// <param name="agendaInfo">The XML agenda info for a talk</param>
        /// <returns>-1 if talk not in the DB, otherwise a unique identifier</returns>
        [OperationContract]
        int FindTalk(string agendaInfo);

        /// <summary>
        /// Returns true if the id points to a valid talk
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        bool ValidTalkID(int id);

        /// <summary>
        /// Mostly for testing purposes - changes the database that is keeping track
        /// of all the talks.
        /// </summary>
        /// <param name="file"></param>
        [OperationContract]
        void SetDBFile(FileInfo file);

        /// <summary>
        /// Returns true if this talk can be served up by the web server right now (i.e. it is in the right state!).
        /// </summary>
        /// <param name="id"></param>
        [OperationContract]
        bool CanBeViewed(int id);

        /// <summary>
        /// Returns default DateTime or it returns the time the conference is scheduled to end.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        DateTime GetConferenceEndDate(int id);

        /// <summary>
        /// Resets the state of all talks in the "Rendering" state to "unrendered" - so will re-queue them.
        /// </summary>
        /// <returns>List of id's of the talks that were reset</returns>
        [OperationContract]
        int[] ResetRenderingTalks();

        /// <summary>
        /// Resets a particular talk to be unrendered
        /// </summary>
        /// <param name="id"></param>
        [OperationContract]
        void ResetTalkStateToUnrendered(int id);

        /// <summary>
        /// Removes a talk from the database - doesn't do anything else, just removes
        /// its record.
        /// </summary>
        /// <param name="id"></param>
        [OperationContract]
        void RemoveTalk(int id);

        /// <summary>
        /// Returns the talk info for a particular talk ID.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        string GetTalkXML(int id);

        /// <summary>
        /// Returns the next time we will be doing an update on this talk, or null
        /// if none is scheduled.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract]
        DateTime? GetNextUpdateTime(int id);
    }

    /// <summary>
    /// Contains basic info for a talk that has been submitted to the database.
    /// </summary>
    [DataContract]
    public class BasicTalkInfo
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public DateTime StartDate {get; set;}
    };
}
